﻿/**
//  MessageEntity.cs
//  Marquee Moment
//  Copyright 2011 Systematix Technocrates. All rights reserved.
**/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace MarqueeMoment.Entities
{
    /// <summary>
    /// This is the class to keep all the transaction method from tblMessage
    /// </summary>
    public class MessageEntity
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        #region Fields

        private Int32 messageId;
        private String messageName;
        private Int32 allowName;

        private List<MessageEntity> messagelist;

        #endregion Fields

        #region Constructors

        public MessageEntity()
            : base()
        {
            messageId = 0;
            messageName = String.Empty;
            allowName = 0;
            
            messagelist = new List<MessageEntity>();
        }


        #endregion Constructors

        #region Properties

        public Int32 MessageId
        {
            get { return messageId; }
            set { messageId = value; }
        }

        public String MessageName
        {
            get { return messageName; }
            set { messageName = value; }
        }


        public Int32 AllowName
        {
            get { return allowName; }
            set { allowName = value; }
        }

        #endregion Properties

        #region Methods

        #region Public

        #endregion Public

        #region Private

        #endregion Private

        #endregion Methods

        #region Static methods

        #region Public

        /// <summary>
        /// To select all the records from the tblMessage table
        /// </summary>
        /// <returns></returns>     
        public static List<MessageEntity> SelectAll()
        {
            List<MessageEntity> messagelist = null;
            MessageEntity message = null;
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return null;

            cmd.CommandText = "[spSelectAllMessage]";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
                if (dataSet.Tables.Count > 0)
                    if (dataSet.Tables[0].Rows.Count > 0)
                    {
                        messagelist = new List<MessageEntity>();
                        for (int count = 0; count < dataSet.Tables[0].Rows.Count; count++)
                        {
                            message = new MessageEntity();
                            message.MessageId = Convert.ToInt32(dataSet.Tables[0].Rows[count][tblMessage.MessageId]);
                            message.MessageName = Convert.ToString(dataSet.Tables[0].Rows[count][tblMessage.MessageName]);
                            message.AllowName = Convert.ToInt32(dataSet.Tables[0].Rows[count][tblMessage.AllowName]);
                            messagelist.Add(message);
                        }
                    }
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select All Messages");
            }
            finally
            {
                if (dataSet != null)
                    dataSet.Dispose();

                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return messagelist;
        }
        /// <summary>
        /// To select all the records from tblMessage and return Dataset of the records
        /// </summary>
        /// <returns></returns>
        public static DataSet SelectAllMessage()
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return null;

            cmd.CommandText = "[spSelectAllMessage]";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
                if (dataSet.Tables.Count > 0)
                    
                        return dataSet;
                  
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select All Messages");
            }
            finally
            {
                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return null;
        }

        /// <summary>
        /// To delete the records of the tblMessage table
        /// </summary>
        /// <param name="messageId">message Id is the Id to delete the Particular Record /param>
        public static void Delete(int messageId)
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return;

            cmd.CommandText = "[spDeleteMessage]";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@" + tblMessage.MessageId, messageId);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Delete Message/Messages");
            }
            finally
            {
                if (dataSet != null)
                    dataSet.Dispose();

                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
        }

        /// <summary>
        /// To update the record of the tblMessage
        /// </summary>
        /// <param name="message">This contains the message Id to update the record</param>
        public static void Update(MessageEntity message)
        {
           
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return;

            cmd.CommandText = "[spUpdateMessage]";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@" + tblMessage.MessageId, message.MessageId);
            cmd.Parameters.AddWithValue("@" + tblMessage.MessageName, message.MessageName);
            cmd.Parameters.AddWithValue("@" + tblMessage.AllowName, message.AllowName);
           
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);

            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Update Message");
            }
            finally
            {
                if (dataSet != null)
                    dataSet.Dispose();

                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
        }
        /// <summary>
        /// To insert the record in tblMessage
        /// </summary>
        /// <param name="message">It contains the information to insert the record i.e name of the message</param>
        public static void Insert(MessageEntity message)
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return;

            cmd.CommandText = "[spInsertMessage]";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@" + tblMessage.MessageName, message.MessageName);
            cmd.Parameters.AddWithValue("@" + tblMessage.AllowName, message.AllowName);
          
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);

            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Insert Message");
            }
            finally
            {
                if (dataSet != null)
                    dataSet.Dispose();

                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
        }
        /// <summary>
        /// To count the total record of the tblMessage
        /// </summary>
        /// <returns></returns>
        public static Int32 SelectRowCountMessage()
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return 0;

            cmd.CommandText = "[spSelectRowCountMessage]";
            cmd.CommandType = CommandType.StoredProcedure;
            #endregion Initialization
            
            try
            {
                return (int)cmd.ExecuteScalar();
            }

            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select RowCount Message");
            }
            finally
            {
                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return 0;            
        }

        /// <summary>
        /// To select the Message from tblMessage table
        /// </summary>
        /// <param name="sqlQuery">It contains the sqlquary to select the records from the table</param>
        /// <returns></returns>
        public static DataSet SelectSQLMessage(String sqlQuery)
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            if (cmd.Connection == null)
                return null;

            cmd.CommandText = sqlQuery;
            cmd.CommandType = CommandType.Text;
            
           SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
                return dataSet; 
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select Message");
            }
            finally
            {
                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return null;            
        }
        /// <summary>
        /// Select MessageName and MessageId from tblMessage
        /// </summary>
        /// <returns></returns>
        public static DataTable SelectAllMessageNameWithId()
        {
            DataTable dt = null;
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            DataSet dataSet = new DataSet();

            cmd.Connection = Connection.GetConnection();
            cmd.CommandText = "[spSelectAllMessageNameWithId]";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
                if (dataSet.Tables.Count > 0)
                    if (dataSet.Tables[0].Rows.Count > 0)
                    {
                        dt = dataSet.Tables[0];

                    }
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select All Message Name With Id");
            }
            finally
            {
                if (dataSet != null)
                    dataSet.Dispose();
                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return dt;
        }

        /// <summary>
        /// Select MessageName and No of Names from tblMessage
        /// </summary>
        /// <returns></returns>
        public static DataTable SelectAllMessageNameWithAllowName()
        {
            DataTable dt = null;
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            DataSet dataSet = new DataSet();

            cmd.Connection = Connection.GetConnection();
            cmd.CommandText = "[spSelectAllMessageNameWithAllowName]";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            #endregion Initialization

            try
            {
                adapter.Fill(dataSet);
                if (dataSet.Tables.Count > 0)
                    if (dataSet.Tables[0].Rows.Count > 0)
                    {
                        dt = dataSet.Tables[0];

                    }
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at Select All Message Name With AllowName");
            }
            finally
            {
                if (dataSet != null)
                    dataSet.Dispose();
                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return dt;
        }
        /// <summary>
        /// To not to save the duplicate record in the tblMessage table
        /// </summary>
        /// <param name="message">This contains the information of message like MessageName and MesaageId</param>
        /// <returns></returns>
        public static bool IsExistMessage(MessageEntity message)
        {
            #region Initialization
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Connection.GetConnection();
            cmd.CommandText = "[spSelectExistingMessage]";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@" + tblMessage.MessageName, message.MessageName.Trim());
            cmd.Parameters.AddWithValue("@" + tblMessage.MessageId, message.MessageId);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            #endregion Initialization

            try
            {
                Int32 existKey = Convert.ToInt32(cmd.ExecuteScalar());
                if (existKey > 0)
                {
                    return true;
                }
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error at CheckValidation in tblMessage");
            }
            finally
            {
                Connection.CloseConnection(cmd.Connection);
                cmd.Dispose();
            }
            return false;
        }
        
        #endregion Public

        #endregion Static methods
    }
}
